package com.nero.controller;

import com.nero.bean.PO.TbUser;
import com.nero.common.pojo.TaotaoResult;
import com.nero.common.utils.CookieUtils;
import com.nero.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Nero on 18/8/18.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    IUserService userServiceImpl;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public TaotaoResult login(TbUser form, HttpServletRequest request, HttpServletResponse response) throws IOException {
        TaotaoResult result = userServiceImpl.Login(form);
        if (result.getStatus() == 200) {
            String token = result.getData().toString();
            CookieUtils.setCookie(request, response, CookieUtils.COOKIE_TOKEN_KEY, token);
        }
        return result;
    }
}